<?php

class Bizprofile_Widgets_Map_Widget extends Tomato_Widget
{
    protected function _prepareShow()
    {
		$front = Zend_Controller_Front::getInstance();
		$request = $front->getRequest(); 
		$params  = $request->getParams();	

		$has_dialog = $this->_request->getParam('has_dialog',false);
		$this->_view->assign('has_dialog',$has_dialog);
		
		$has_flash  = $this->_request->getParam('has_flash',true);
		$this->_view->assign('has_flash',$has_flash);
		
		$hasLoginEnterprise = null;
		$hasParnerEnterprise = null;
		$hasFrontEndActive   = null;
        $enterprise_id = null;
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            if($user->enterprise){
                $enterprise_id = $user->enterprise->id;
				$hasLoginEnterprise = true;
            }
        }
        if(isset($params['enterprise_id'])){
            $enterprise_id = $params['enterprise_id'];
			$hasParnerEnterprise = true;	
        }

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Gallery models access to db
         */
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);

        /**
         * Media file access to data
         **/
        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
        $fileDao->setDbConnection($conn);

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);
		$enterprise = null;
		$showMap  = null;
        if($enterprise_id){
            $enterprise = $enterpriseDao->getById($enterprise_id);
			if($hasParnerEnterprise && $enterprise->frontend_active==1)
				$hasFrontEndActive = true;
            /**
            * Get enterprise address on map
            **/
            /*$mapUrl  = "http://maps.google.com/maps/api/staticmap?center=";
            $mapUrl .= $enterprise->contact_address;
            $mapUrl .= "&zoom=15&size=296x260&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false";
            $this->_view->assign('mapUrl', $mapUrl);*/

            /**
            * Get set for selectbox
            **/
            $sets = $galleryDao->getSetInfo($enterprise_id);

            /**
            * Get 5 latest image of all album belong to enterprise
            **/
            $arrPhotos = array();
            $i = 1;
            
            foreach($sets as $set){
                $photos = $fileDao->getFilesInSet($set->set_id, 0, 5, 1, 1);
                if(count($photos) > 0){
                	//echo count($photos).", ";
                    foreach($photos as $photo){
                        $arrPhotos[$i][0] = $photo->file_id;
                        $arrPhotos[$i][1] = $photo;
                        if($i > 5){
                            break;
                        }
                        $i++;
                    }
                }
            }
            //echo count($arrPhotos);
            $this->_view->assign('num_Photos', count($arrPhotos));
            
            array_multisort($arrPhotos, SORT_DESC);
            
            $mapUrl = $this->_view->APP_STATIC_SERVER.'/images/maps/'.$enterprise_id.'.png';
            $imageString = "img1={$mapUrl}?time=".time();
            $i = 2;
            foreach($arrPhotos as $photo){
                $imageString .= '&img'.$i++.'='.$photo[1]->image_crop;
            }
            $this->_view->assign('imageString', $imageString);
        }
		$showMap = null;
		if($hasParnerEnterprise){
			if($hasFrontEndActive) $showMap = true;
		}else{
			if($hasLoginEnterprise) $showMap = true;
		}
		$this->_view->assign("showMap",$showMap);
        $this->_view->assign('enterprise_id', $enterprise_id);
		$this->_view->assign("enterprise",$enterprise);
	}

    protected function _prepareConfig()
    {

    }
}
